{% extends "tj3/base.html" %}

{% block navigation %}
	<script type="text/javascript">
		$('document').ready(function(){
		
		$(".gantt").gantt({
			source: "{%url task_gantt_json project.id 2 %}",
			itemsPerPage: 50,
			months: ["January", "February", "March",
					"April", "May", "June", "July",
					"August", "September", "October",
					"November","December"],
			dow: ["Su","M","Tu","W","Th","F","Sa"],
			navigate: 'scroll',
			scale: 'days'
		})


			$('#email_sent_list').hide();
			$('#myad').hide();
						
			$('#email').click(function(){
				var proj_id;
				var scenario_id;
				var additional_emails;
				var to_resource;
				var checked = $('input[type=checkbox]').is(':checked');
				
				additional_emails=$('textarea[name=email]').val();				
				// Post the new event to the server!
				var json_event=$.toJSON({'proj_id':{{project.id}}, 'scenario_id':{{scenario.id}}, 
					'emails':additional_emails, 'to_resource':checked});
				$.post("{% url project_email %}",
				{'data':json_event},
				function (data){
					alert(data);
					for(var i=0; i < data.length; i++){
						var first_name=data[i][0];
						var last_name=data[i][1];
						var email=data[i][2];
						
						msg='Reminder for '+first_name+'.'+last_name+' have been sent to '+email;
						$('#email_receiver').prepend('<li>'+msg+'</li>');
					}

					if (data.length>0){
						$('#email_sent_list').show();
					}
					//window.location.replace('/tj3');
				});          
			});
			
			$('#rebalance_btn').click(function(){				
				$('#myad').cycle({ 
			    	fx:    'fade', 
			        speed:  2500 
			    }).modal({
					opacity:80,
					overlayCss: {backgroundColor:"#fff"}
				}).show();
				$.ajax({
					type: 'post',
					url: "{% url project_compile project.id, scenario.id %}",
					success: function(){
						$('#myad').hide();
						},
					error: function (request, status, error) {
						alert(request.responseText);
						$('#myad').hide();
					}
				});	

				
			});
			
			$("#my_tabs").tabs();
		});
	</script>

{% endblock navigation %}


{% block content %}
<h1 class="ui-widget ui-widget-header">{{project.name}}<span class="float_right">Scenario: {{scenario.name|capfirst}}</span></h1>

<table>
<tr><td width="50%" valign="top">
	<div class="ui-widget ui-widget-content ui-corner-all">
		<div class="mycontent">
			<h1 class="ui-state-highlight">Tasks Due</h1>
	
			{% if due|length > 0 %}
			{% regroup due by end as due_list %}
				{% for end in due_list %}
    				<span class="ui-icon ui-icon-pin-s" style="float: left; margin-right: .3em;"></span>
    				<b>{{ end.grouper }}</b>
    				<br />
        				{% for item in end.list %}
        					<div class="prefix-1">
            					<a href={% url task_detail item.task.id %}>
            						{{ item.task.name }}
            					</a> 
            					<span class="float_right">({{item.completion}}%)</span>
            				</div>
            				<div>
            					{% for r in item.resources %}
            						<p class="prefix-2 fancy">
            						<a href="{% url project_calendar item.task.project.id,item.task.id,r.id %}" title="Log a Booking">
            						{{r.first_name|capfirst}} {{r.last_name|capfirst}}
            						</a>
            					{% endfor %}
            				</div>
            			{% endfor %}
				{% endfor %}
			
			{% else %}
				<span>No work due to complete today!</span>
			{% endif %}
		</div>
		
		{% if missing %}
			<div class="mycontent">
				<h1 class="ui-state-highlight">Missing Status</h1>
				There are <span class="ui-state-error ui-corner-all">{{missing}}</span> tasks missing status. 
				<br>
				<br>
				<p>You can refresh task statuses by using the "Rebalance" function.</p>
			</div>
		{% endif %}
		
	</div>
<td width="50%" valign="top">
	<div id="my_tabs">
		<ul>
			<li><a href="#tabs-1">Rebalance</a></li>
			{% if ready %}
				<li><a href="#tabs-2">Task Reminder Email</a></li>	
			{% endif %}
		</ul>
	
		<div id="tabs-1">
			<div class="mycontent">
				Rebalancing the project will scan the entire project definitions and update task statues
				based on the project plan and actual timesheets. It is important to note that this process can be time
				consuming depending on the size of your project. Further, the rebalancing will fail if constraints
				defined by the project plan can not be met. A most common cause for such failure is that calendar date
				is continuously changing, which will break task dependencies in SS/SF/FS/FF relationships.

				<br />		
				<br />		
				{% if last_modified %}
					<div>
						Last rebalance was run successfully on {{last_modified}}.
					</div>
				{% else %}
					<div class="ui-state-error">Rebalance has never been successful for this scenario. Please
					review the <a href="{% url project_main_plan project.id %}">Project Planning</a> and try again.</div>
				{% endif %}
			</div>

			<button class="ui-state-default ui-corner-all float_right" id="rebalance_btn">
				<span class="ui-icon ui-icon-refresh" style="float: left; margin-right: .3em;"></span>
				<a href="{% url project_compile project.id, scenario.id %}">Rebalance</a>
			</button>
		</div>
			
		{% if ready %}
		<div id="tabs-2">
			<div class="mycontent">
				<div id="email_ack" class="ui-state-alert"></div>
				<p>You can send out a task reminder to the emails registered with each resource account. 
				The person will see which task to work on each day according to the project plan.
			
				<br />
				<br />
				Checking the "Resource" will send the emails to the assigned resource.
				<br />
	    		<input type="checkbox" name="to_resource" value="resource">Resource</a>
	    		<br />
	    		<br />
	    		<p>You can specify additional emails who would receive a copy of the reminder email
	    		besides the assigned resource. Multiple emails are separated by a comma.
	    		<br />
	    		<textarea name="email" rows="5" cols="40"></textarea><br />
	    	</div>
	    	                        	
			<button class="ui-state-default ui-corner-all float_right">
				<span class="ui-icon ui-icon-mail-closed" style="float: left; margin-right: .3em;"></span>
				<a href="#" id="email">Send Task Reminder Emails</a>
			</button>
			
			<div id="email_sent_list">
				<br />
				<br />
				<h2 class="ui-state-highlight ui-corner-all">Reminder Sent To</h2>
				<ul id="email_receiver">			
				</ul>
			</div>
		</div>		
		{% endif %}
	</div>
</table>

<div class="span-17 last">
<div class="gantt"></div>
</div>

<div class="span-17 last">
	<h2 class="ui-state-highlight ui-corner-all">Go To</h2>
	<ul>
		<li><a href={% url home %}><b>Home</b></a>
		<li><a href={% url project_main_plan project.id %}><b>Project Planning</b></a>
		<li>Project Report:
			<ul> 
			{% for c in comparables %}
				<li><a href={% url taskreport_view project.id, c.id, scenario.id %}>
				{{c.name|capfirst}} vs. {{scenario.name|capfirst}}
				</a>
			{% endfor %}
			</ul>
		<li><a href={% url journal_display project.id %}>Project Journal</a>
		{% if ready %}
			<li><a href={% url project_performance project.id,scenario.id %}>Performance Monitors</a>
			<li>Reports:
			<ul>
				{% for r in reports %}
					<li><a href={%url report_display r.id %}>{{r.template.name}}</a>
				{% endfor %}
			</ul>
		{% endif %}
	</ul>
	
</div>


<div id="myad" class="pics"> 
	<img src="http://static.howstuffworks.com/gif/smart-car-1.jpg" width="200" height="200" /> 
	<img src="http://static.howstuffworks.com/gif/smart-car-2.jpg" width="200" height="200" /> 
	<img src="http://static.howstuffworks.com/gif/smart-car-3.jpg" width="200" height="200" /> 
</div> 
{% endblock content %}
